package cn.tedu.file;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

public class FileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("./test.txt");
        String name = file.getName();
        System.out.println(name);
        long length = file.length();
        System.out.println(length);
        boolean cw = file.canWrite();
        System.out.println(cw);
        boolean cr = file.canRead();
        System.out.println(cr);
        boolean hi = file.isHidden();
        System.out.println(hi);
        if (file.exists()){
            System.out.println("该文件已存在");
        }else{
            file.createNewFile();
            System.out.println("文件创建成功");
        }
        File file1 = new File("test.txt");
        if (file1.exists()){
            file.delete();
            System.out.println("该文件已删除");
        }else {
            System.out.println("该文件不存在.");
        }
        File file2 = new File("./demo");
        if (file2.exists()){
            System.out.println("该目录已经存在.");
        }else{
            System.out.println("目录不存在,则创建");
            file2.mkdir();
        }
        File file3 = new File("./a/b/c/d/e/demo");
        if (file3.exists()){
            System.out.println("该目录已经存在");
        }else{
            file3.mkdirs();
            System.out.println("已创建");
        }
        File file4 = new File(".");
        if (file4.isDirectory()){
            File[] files = file4.listFiles();
            System.out.println("当前目录包含"+files.length+"个子项");
            for (int i=0;i<files.length;i++){
                File file5 = files[i];
                System.out.println(file5.getName());
            }
        }else {
            System.out.println("不是个目录");
        }
        File file5 = new File("src/main/java/cn/tedu/file");
        if (file5.isDirectory()){
            File[] files = file5.listFiles();
            System.out.println("当前目录包含"+files.length+"个子项");
            for (int i=0;i<files.length;i++){
                File file6 = files[i];
                System.out.println(file6.getName());
            }
        }else {
            System.out.println("不是个目录");
        }
        File file6 = new File(".");
        if (file6.isDirectory()){
            File[] files = file6.listFiles();
            System.out.println("当前目录中包含"+file6.length()+"个子项");
            for (int i=0;i<files.length;i++){
                File file7 = files[i];
                System.out.println(file7.getName());
            }
            File[] dus = file6.listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return file.getName().startsWith("f");
                }
            });
            System.out.println(dus.length);
            System.out.println("运行完毕");
        }

    }
}
